/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package slicktemplate;

import java.awt.geom.Point2D;
import java.io.Serializable;
import java.util.ArrayList;
import org.newdawn.slick.*;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

/**
 *
 * @author Josh
 */
public class TurretDynamite extends Collideable implements Renderable, Updateable, Attackable, Serializable {

    float x, y;
    BasicGameState currentLevel = null;
    public static final ArrayList<TurretDynamite> all = new ArrayList<TurretDynamite>();
    SerializableImage sprite;
    static final int reload = 120;
    static final int MAX_HEALTH = 200;
    int health = MAX_HEALTH;
    long steps = 0;
    long lastFired = -reload;
    private Animation anim = new Animation();
    Enemy1 nearest; //just for testing, will be remvoed later
    private boolean[] mouseState = new boolean[3];
    Image sprite1;
    Image sprite2;
    Image sprite3;
    Image sprite4;
    Sound fireSound;

    public TurretDynamite(float x, float y) {
        all.add(this);

        this.x = x;
        this.y = y;
        try {
            sprite1 = new SerializableImage("sprites/Frame1TNT.png"); //try to load the sprite
            sprite2 = new SerializableImage("sprites/Frame2TNT.png"); //try to load the sprite
            sprite3 = new SerializableImage("sprites/Frame3TNT.png"); //try to load the sprite
            sprite4 = new SerializableImage("sprites/Frame4TNT.png"); //try to load the sprite
            
            anim.addFrame(sprite1, 100);
            anim.addFrame(sprite2, 100);
            anim.addFrame(sprite3, 100);
            anim.addFrame(sprite4, 100);
        } catch (Exception e) {
            e.printStackTrace();
        }
        boundingBox = new Rectangle(x, y, sprite1.getWidth() - 1, sprite1.getHeight() - 1);
    }

    public TurretDynamite(float x, float y, BasicGameState currentlvl) {
        all.add(this);
        currentLevel = currentlvl;
        System.out.println("created");
        this.x = x;
        this.y = y;
        try {
            sprite = new SerializableImage("sprites/dynamiteTower.png"); //try to load the sprite
        } catch (Exception e) {
            e.printStackTrace();
        }
        boundingBox = new Rectangle(x, y, sprite1.getWidth() - 1, sprite1.getHeight() - 1);
    }

    @Override
    public void render(Graphics g) {
        anim.draw(x,y);
        g.setColor(Color.red);
        g.fillRect(x, y, sprite1.getWidth(), 6);
        g.setColor(Color.green);
        g.fillRect(x, y, sprite1.getWidth()*health/MAX_HEALTH, 6);
    }

    @Override
    public boolean update(Input input, StateBasedGame sb) {
        nearest = null;
        steps++;
        if (!Enemy1.all.isEmpty()) {
            double largest = 0;
            for (Enemy1 it : Enemy1.all) {
                if (Point2D.distance(x, y, it.getX(), it.getY()) > largest) {
                    nearest = it;
                    largest = Point2D.distance(x, y, it.getX(), it.getY());
                }
            }
            int rotatedegrees = (int) ((int) (180 / Math.PI) * Math.atan2((nearest.getY() - y), (nearest.getX() - x)));
            anim.getCurrentFrame().setRotation(rotatedegrees);

            if (steps-lastFired > reload) {
                //  fireSound.play();
                new ProjectileDynamite(x + 16, y + 16, 4, MathHelper.point_direction(x, y, nearest.x, nearest.y), 100, this);
                lastFired = steps;
            }
        }
        if(health > 0){
            return false;
        } else {
            return true;
        }
    }

    @Override
    public float getX() {
        return x;
    }

    @Override
    public float getY() {
        return y;
    }

    @Override
    public float getHealth() {
        return 0;
    }

    @Override
    public void subHealth(int hp) {
        health -= hp;
    }

    @Override
    boolean isBuilding() {
        return true;
    }
}
